import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:archive/archive_io.dart';

// 应该会压缩到pwd路径，而不是目标文件夹
extension Zip on Directory {
  zip({required String name}) {
    if (existsSync() == false) throw Exception("文件夹不存在");
    ZipFileEncoder().zipDirectory(this, filename: "$name.zip");
  }
}

extension Unzip on Uint8List {
  Archive unzip() {
    final inputStream = InputStream(this);
    return ZipDecoder().decodeBuffer(inputStream);
  }

  String decode() => utf8.decoder.convert(this);
}

extension File on ArchiveFile {
  bool get isImage =>
      name.endsWith(".png") || name.endsWith(".jpg") || name.endsWith(".jpeg");
  bool get isMarkdown => name.endsWith(".md");
  String get fullname => name.split("/").last;
  String get purename => fullname.split(".").first;
}
